package com.ithuangyl.common.mvp.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ithuangyl.common.base.BaseFragment;
import com.ithuangyl.common.mvp.factory.IPresenterFactory;
import com.ithuangyl.common.mvp.factory.PresenterFactoryImpl;
import com.ithuangyl.common.mvp.proxy.BaseMvpPresenterProxyImpl;
import com.ithuangyl.common.mvp.proxy.IPresenterProxy;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public abstract class BaseMvpFragment<V extends IBaseMvpView, P extends BaseMvpPresenter<V>> extends BaseFragment implements IPresenterProxy<V, P> {
    private static final String TAG = "BaseMvpFragment";
    private static final String PRESENTER_SAVE_KEY_FRAGMENT = "presenter_save_key_fragment";
    /**
     * 创建被代理对象,传入默认Presenter的工厂
     */
    private BaseMvpPresenterProxyImpl<V, P> mProxy = new BaseMvpPresenterProxyImpl<>(PresenterFactoryImpl.<V, P>createFactory(getClass()));

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        mProxy.onCreate((V) this);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mProxy.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(PRESENTER_SAVE_KEY_FRAGMENT, mProxy.onSaveInstanceState());
    }

    @Override
    public void setPresenterFactory(IPresenterFactory<V, P> presenterfactory) {
        mProxy.setPresenterFactory(presenterfactory);
    }

    @Override
    public IPresenterFactory<V, P> getPresenterFactory() {
        return mProxy.getPresenterFactory();
    }

    @Override
    public P getPresenter() {
        return mProxy.getPresenter();
    }
}
